<?php
// +----------------------------------------------------------------------
// | Description: 公共模型,所有模型都可继承此模型，基于RESTFUL CRUD操作
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

class CommonClient extends Model
{
    use \app\Traits\DataList;
    protected $param;
    protected $map = [];
    protected $orderByString; // 排序规则
    protected $specialConditions; // 特殊条件
    protected $fuzzyConditions = null;
    protected $clearConditions = null; // 精确查询字段
    protected $returnFields = '';
    protected $rangeConditions = null;
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = null;

    /**
     * 解析搜索条件
     */
    public function getDataList()
    {
        $this->page = $this->param['page'] or 1;
        $this->limit = $this->param['limit'] or $this->limit;
        $this->orderByString = $this->param['orderByString'];
        // $this->param['IsDelete'] = 0;   // 开启软删除 

        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where($this -> name.'.IsDelete', 0);

        if ($this->limit == -1) {
            $resData = $this->field($this->returnFields)->select();
        } else {
            $resData = $this->field($this->returnFields)->page($this->page, $this->limit)->select();
        }
// p($this -> getlastsql());
        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where('IsDelete', 0);

        $dataCount = $this->count();
        $data['list'] = $resData;
        $data['dataCount'] = $dataCount;
        return $this->filter($data);
    }
    /**
     * 过滤
     *
     * @return void
     */
    public function filter($data)
    {
        return $data;
    }

    /**
     * [getDataById 根据主键获取详情]
     * @linchuangbin
     * @DateTime  2017-02-10T21:16:34+0800
     * @param     string                   $id [主键]
     * @return    [array]
     */
    public function getDataById($id = '')
    {
        $data = $this->get($id);
        if (!$data) {
            $this->error = '暂无此数据';
            return false;
        }
        return $data;
    }

    /**
     * [createData 新建]
     * @linchuangbin
     * @DateTime  2017-02-10T21:19:06+0800
     * @param     array                    $param [description]
     * @return    [array]                         [description]
     */
    public function createData($param)
    {
        // 验证
        $validate = validate($this->name);
        if (!$validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        // p($param);
        // p($this -> getlastsql());
        try {
            $this->data($param)->allowField(true)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = $e -> getMessage();
            return false;
        }
    }

    /**
     * 通过id修改用户
     * @param  array   $param  [description]
     */
    public function updateDataById($param, $id, $fields = true, $pk = 'Id')
    {
        // p(11);
        try {
            $this->allowField($fields)->save($param, [$pk => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '编辑失败';
            return false;
        }
    }

    /**
     * [delDataById 根据id删除数据]
     * @linchuangbin
     * @DateTime  2017-02-11T20:57:55+0800
     * @param     string                   $id     [主键]
     * @param     boolean                  $delSon [是否删除子孙数据]
     * @return    [type]                           [description]
     */
    public function delDataById($id = '', $delSon = false)
    {
        try {
            $this->where('Id', $id)->delete();
            return true;
        } catch (\Exception $e) {
            $this->error = $e -> getMessage();
            return false;
        }
    }

    /**
     * [delDatas 批量删除数据]
     * @linchuangbin
     * @DateTime  2017-02-11T20:59:34+0800
     * @param     array                   $ids    [主键数组]
     * @param     boolean                 $delSon [是否删除子孙数据]
     * @return    [type]                          [description]
     */
    public function delDatas($ids = [], $delSon = false)
    {
        if (empty($ids)) {
            $this->error = '删除失败';
            return false;
        }
        try {
            $this->where('Id', 'in', $ids)->delete();
            return true;
        } catch (\Exception $e) {
            $this->error = $e -> getMessage();
            return false;
        }
    }

    /**
     * [ 软删除数据 ]
     * @linchuangbin
     * @DateTime  2017-02-11T20:59:34+0800
     * @param     array                   $ids    [主键数组]
     * @param     boolean                 $delSon [是否删除子孙数据]
     * @return    [type]                          [description]
     */
    public function SoftdelDatas($ids = [], $delField = 'IsDelete', $value = 1)
    {
        if (empty($ids)) {
            $this->error = '请传入要删除的数据';
            return false;
        }

        try {
            $this->where('Id', 'in', $ids)->setField($fieldName, $value);
            return true;
        } catch (\Exception $e) {
            $this->error = $e -> getMessage();
            return false;
        }
    }


    /**
     * [enableDatas 批量启用、禁用]
     * @AuthorHTL
     * @DateTime  2017-02-11T21:01:58+0800
     * @param     string                   $ids    [主键数组]
     * @param     integer                  $status [状态1启用0禁用]
     * @param     string                   $fieldName [要修改状态的字段名称 默认status]
     * @return    [type]                           [description]
     */
    public function enableDatas($ids = [], $value = 1, $fieldName = "status")
    {
        if (empty($ids)) {
            $this->error = '请传入要修改的条目';
            return false;
        }

        try {
            $this->where('Id', 'in', $ids)->setField($fieldName, $value);
            return true;
        } catch (\Exception $e) {
            $this->error = $e -> getMessage();
            return false;
        }
    }
}
